package gov.pbc.nn.kjgl.maintenance.view;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.Expose;
import com.bstek.uflo.client.service.ProcessClient;
import com.bstek.uflo.client.service.TaskClient;
import com.bstek.uflo.model.task.Task;
import com.bstek.uflo.service.TaskOpinion;

import gov.pbc.nn.core.dao.CommonHibernateDao;
import gov.pbc.nn.core.dao.PbcnnCoreHibernateDao;
import gov.pbc.nn.kjgl.maintenance.entity.SysEmergencyScheme;
import gov.pbc.nn.uflo.entity.Opinion;

@Component
public class SysjszgShPR {

	@Resource(name = CommonHibernateDao.BEAN_ID)
	private CommonHibernateDao hibernateDao;
	@Resource(name = PbcnnCoreHibernateDao.BEAN_ID)
	private PbcnnCoreHibernateDao coreHibernateDao;
	@Resource(name = ProcessClient.BEAN_ID)
	private ProcessClient processClient;
	@Resource(name = TaskClient.BEAN_ID)
	private TaskClient taskClient;
	@Autowired
	private SchemeJdbcDao schemeJdbcDao;
	
	@DataProvider
	public SysEmergencyScheme getAll(String businessId) {
		return schemeJdbcDao.loadProject(businessId);
	}

	@Expose
	@Transactional
	public void ajaxJszgSh(String taskId, String opinion, int agree) {
		/**
		 * 科技处处长审核通过。流程变量agree=1 代表 同意 agree=0 代表不同意
		 */
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		// 开始流程
		taskClient.start(Long.valueOf(taskId));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("agree", agree);
		Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		o.setBusTable("T_SYS_EMERGENCY_SCHEME");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setNodeName(task.getNodeName());
		o.setProcessName("应急预案");
		o.setProcessInstanceId(task.getProcessInstanceId());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		o.setCreateUser(ContextHolder.getLoginUserName());
		o.setCreateDate(new Date());
		this.coreHibernateDao.save(o);
		taskClient.complete(Long.valueOf(taskId), map, option);
	}

	@Expose
	@Transactional
	public void ajaxYwbmZgSh(String taskId, String opinion, SysEmergencyScheme syses, int agree) {
		/**
		 * 科技处处长审核通过。流程变量agree=1 代表 同意 agree=0 代表不同意
		 *  修改state = 10 代表著已經完成
		 */

		
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		// 开始流程
		taskClient.start(Long.valueOf(taskId));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("agree", agree);
		Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		o.setBusTable("T_SYS_EMERGENCY_SCHEME");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setProcessName("应急预案");
		o.setNodeName(task.getNodeName());
		o.setProcessInstanceId(task.getProcessInstanceId());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		o.setCreateUser(ContextHolder.getLoginUserName());
		o.setCreateDate(new Date());
		this.coreHibernateDao.save(o);
		if(agree==1){
			syses.setState(10);//修改state = 10 代表已经完成
			this.hibernateDao.update(syses);
		}
		taskClient.complete(Long.valueOf(taskId), map, option);
	}

}
